#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 2e5 + 10;

int n, x[N], y[N];

mt19937 rnd(19491001);

struct valpot {
	int t[N], tot;
	void reset() { tot = 0; }
	void ins(int x) { t[++tot] = x; }
	void build() { sort(t + 1, t + tot + 1), tot = unique(t + 1, t + tot + 1) - t - 1; }
	int operator [](int x) { return lower_bound(t + 1, t + tot + 1, x) - t; }
} a, b;

int cnt, fa[N << 1];
veg ans;
veg G[N << 1];
bool vis[N << 1], pvis[N];

void pit(int x, int y) { if(x == -1 || y == -1) return; cnt += ::x[x] == ::x[y] || ::y[x] == ::y[y]; pvis[x] = pvis[y] = 1, ans.eb(x, y); }

int dfs(int x, int lst) {
	vec pot; vis[x] = true;
	for(auto [y, id] : G[x]) if(y != fa[x]) {
			if(!vis[y]) { fa[y] = x; if(dfs(y, id) != -1) pot.eb(id); }
			else if(!pvis[id]) pot.eb(id);
		}
	if(pot.size() & 1) pot.eb(lst), lst = -1;
	int ret = 0;
	for(auto v : pot) if(!ret) ret = v; else pit(ret, v), ret = 0;
	return lst;
}

void solve() {
	a.reset(), b.reset();
	n = in * 2; rep(i, 1, n) x[i] = in, y[i] = in, a.ins(x[i]), b.ins(y[i]);
	a.build(), b.build(); int at = a.tot, bt = b.tot;
	rep(i, 1, n) x[i] = a[x[i]], y[i] = b[y[i]], pvis[i] = 0;
	cnt = 0; ans.clear();
	rep(i, 1, at + bt) G[i].clear(), vis[i] = 0, fa[i] = 0;
	rep(i, 1, n) {
		G[x[i]].eb(y[i] + at, i);
		G[y[i] + at].eb(x[i], i);
	}
	rep(i, 1, at + bt) if(!vis[i]) dfs(i, -1);
	int ret = 0;
	rep(i, 1, n) if(!pvis[i]) { if(!ret) ret = i; else pit(ret, i), ret = 0; }
	printf("%d\n", cnt); for(auto v : ans) printf("%d %d\n", v.fi, v.se);
}

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	for(int T = in; T; T--) solve(); return 0;
}
